<?php
// +----------------------------------------------------------------------
// | Azthink通用开发平台 [ TOMORROW IS ANOTHER DAY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2019 http://www.azthink.top All rights reserved.
// +----------------------------------------------------------------------
// | Author 李忠<ny_lizhong@126.com>
// +----------------------------------------------------------------------

namespace app\admin\model;

use think\facade\Request;
use think\Model;

/**
 * 用户模型
 * @author 李忠<ny_lizhong@126.com>
 * @createAt 2018-06-20 17:55:59
 */
class Member extends Model {
    // 设置当前模型对应的完整数据表名称
    protected $table = '__SYS_MEMBER__';
    protected $salt = '';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    //数据自动完成
    protected $insert = ['salt', 'password', 'reg_ip', 'reg_time', 'office_id'];

    //关联

    //修改器
    public function setSaltAttr() {
        $this->salt = rand_string();
        return $this->salt;
    }

    public function setPasswordAttr($value) {
        return md5($value . $this->salt);
    }

    public function setRegTimeAttr() {
        return time();
    }

    public function setRegIpAttr() {
        return Request::ip();
    }

    public function setOfficeIdAttr($value){
        return CID;
    }


    public function getStatusTextAttr($value, $data) {
        return get_status_html($data['status']);
    }

    public function getTypeTextAttr($value, $data) {

        return get_config_text('member_type', $data['type']);
    }


    //自定义方法
    public static function lists($title, $status, $office) {
        $map = array();
        if ($status != null && $status !== '') {
            array_push($map, ['a.status', '=', $status]);
        } else {
            array_push($map, ['a.status', '>=', 0]);
        }

        if ($office !== '' && !empty($office)) {
            array_push($map, ['a.office_id', '=', $office]);
        }

        if ($title !== '' && !empty($title)) {
            array_push($map, ['a.title|a.name', 'like', '%' . $title . '%']);
        }
        return self::alias('a')->join('__SYS_OFFICE__ b', 'a.office_id=b.id')->where($map)
            ->field('a.*, b.title as office_title')->order('a.id desc')->paginate();
    }

    public static function login($name, $password) {
        $map['a.name'] = $name;
        $map['a.status'] = 1;

        $member = self::alias('a')
            ->join('__SYS_OFFICE__ b', 'a.office_id = b.id')
            ->where($map)
            ->field('a.id,a.name,a.title,a.office_id,b.title as office_title,a.password,a.salt,a.login,a.status')
            ->find();
        if ($member && $member['status']) {
            /* 验证用户密码 */
            if (md5($password . $member['salt']) === $member['password']) {
                self::autoLogin($member);
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public static function casLogin($name) {
        $map['a.name'] = $name;
        $map['a.status'] = 1;
        $member = self::alias('a')
            ->join('__SYS_OFFICE__ b', 'a.office_id = b.id')
            ->where($map)
            ->field('a.id,a.name,a.title,a.office_id,b.title as office_title,a.password,a.salt,a.login,a.status')
            ->find();
        if ($member) {
            self::autoLogin($member);
            return true;
        } else {
            return false;
        }

    }


    public static function modifyPwd($password, $id) {

        $data = array();
        $data['salt'] = rand_string();
        $data['password'] = md5($password . $data['salt']);
        return self::where('id', $id)->update($data);
    }


    /* 自动登录用户 */
    private static function autoLogin($member) {
        /* 更新登录信息 */
        $data = array(
            'name' => $member['name'],
            'login' => $member['login'] + 1,
            'last_login_time' => time(),
            'last_login_ip' => get_client_ip(),
        );

        self::where(array('id' => $member['id']))->update($data);


        /* 登录记录 session和cookies*/
        $auth = array(
            'id' => $member['id'],
            'name' => $member['name'],
            'title' => $member['title'],
            'office_id' => $member['office_id'],
            'office_title' => $member['office_title'],
            'last_login_time' => $data['last_login_time'],
        );
        session('user_auth', $auth);
        session('data_auth_sign', data_auth_sign($auth));
    }

    public static function state($id, $status) {
        return self::where('id', $id)->update(['status' => $status]);
    }

}
